<?php

namespace App\Models\User;

use App\Models\User\Repositories\Interfaces\WechatMiniAppUserRepositoryInterface;
use App\Models\User\Repositories\Interfaces\WechatOfficialUserRepositoryInterface;
use App\Trip\Auth\JWT\Contracts\JWTSubject;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;

/**
 * App\Models\User\User
 *
 * @property int
 *               $id
 * @property string
 *               $user_id  uuid
 * @property string
 *               $unionid  微信unionid
 * @property string
 *               $phone    电话
 * @property string
 *               $nickname 昵称
 * @property string
 *               $avatar   头像
 * @property Carbon|null
 *               $created_at
 * @property Carbon|null
 *               $updated_at
 * @property-read DatabaseNotificationCollection|DatabaseNotification[]
 *                $notifications
 * @property-read int|null
 *                    $notifications_count
 * @method static Builder|User newModelQuery()
 * @method static Builder|User newQuery()
 * @method static Builder|User query()
 * @method static Builder|User whereAvatar($value)
 * @method static Builder|User whereCreatedAt($value)
 * @method static Builder|User whereId($value)
 * @method static Builder|User whereNickname($value)
 * @method static Builder|User wherePhone($value)
 * @method static Builder|User whereUnionid($value)
 * @method static Builder|User whereUpdatedAt($value)
 * @method static Builder|User whereUserId($value)
 * @mixin Eloquent
 */
class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable;

    const CLIENT_TYPE_WECHAT_MINIAPP  = 1;
    const CLIENT_TYPE_WECHAT_OFFICIAL = 2;
    protected $fillable
        = [
            'user_id',
            'unionid',
            'phone',
            'nickname',
            'avatar',
        ];

    public static function cacheKey(string $user_id): string
    {
        return "User:userInfo:{$user_id}";
    }

    public function getPhone(): string
    {
        return $this->getAttribute('phone') ?: '';
    }

    public function getNickName(): string
    {
        return $this->getAttribute('nickname') ?: '';
    }

    public function getAvatar(): string
    {
        return $this->getAttribute('avatar') ?: '';
    }

    public function getUnionId(): string
    {
        return $this->getAttribute('unionid');
    }

    public function getAuthIdentifier(): string
    {
        return $this->getUserId();
    }

    public function getUserId(): string
    {
        return $this->getAttribute('user_id');
    }

    /**
     * auth sub=> 键名
     *
     * @return string
     */
    public function getAuthIdentifierName(): string
    {
        return 'user_id';
    }

    public function getJWTIdentifier()
    {
        return $this->getUserId();
    }

    public function getJWTCustomClaims(): array
    {
        return [
            'avatar' => $this->getAttribute('avatar'),
            //            'id'      => $this->id,
            //            'user_id' => $this->getUserId(),
        ];
    }

    public function getMiniAppUser(string $appid = ''): ?WechatMiniAppUser
    {
        $repo = app(WechatMiniAppUserRepositoryInterface::class);
        return $repo->findByAppIdAndUserId($this->getUserId(), $appid);
    }

    public function getOfficialUser(string $appid = ''): ?WechatOfficialUser
    {
        $repo = app(WechatOfficialUserRepositoryInterface::class);
        return $repo->findByAppIdAndUserId($this->getUserId(), $appid);
    }
}
